﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using PalFramework.Object;

namespace PalBattleEmulator.Graphic
{
    /// <summary>
    /// EffectGraphic.xaml 的交互逻辑
    /// </summary>
    public partial class EffectGraphic : UserControl, IGraphicElement
    {
        protected IEffect effect;
        protected GraphicFrame frame;
        protected List<BitmapSource> sources;

        private bool finished;

        public bool Finished
        {
            get { return finished; }
            set { finished = value; }
        }

        public EffectGraphic()
        {
            InitializeComponent();
        }

        public virtual void Initialize(IEffect effect)
        {
            this.effect = effect;

            //填充source
            sources = new List<BitmapSource>();
            string imageIndex = effect.Id;
            for (int i = 0; ; i++)
            {
                BitmapSource img = (BitmapSource)ContextModules.EffectImageManager.GetImage(imageIndex, i);
                if (img == null) break;
                sources.Add(img);
            }

            //初始化frame
            frame = new GraphicFrame();
            frame.FrameIndex = -1;
            frame.PosX = effect.PosX;
            frame.PoxY = effect.PosY;
            frame.ZIndex = effect.ZIndex;
            frame.RepeatTimes = effect.RepeatTimes;
            frame.Delay = effect.Delay;

        }

        public virtual void Draw()
        {
            if (Finished) return;

            if (frame.Delay > 0) frame.Delay--;
            else
            {
                frame.FrameIndex++;
                if (frame.FrameIndex >= sources.Count)
                {
                    if (frame.RepeatTimes > 0)
                    {
                        frame.RepeatTimes--;
                        frame.FrameIndex = 0;
                    }
                    else
                    {
                        //finish
                        Finished = true;
                    }
                }
                frame.Delay = effect.Speed;
            }

            this.Dispatcher.Invoke(new drawFrameDelegate(drawFrame), System.Windows.Threading.DispatcherPriority.Normal, frame);
        }


        protected delegate void drawFrameDelegate(GraphicFrame frame);

        protected void drawFrame(GraphicFrame frame)
        {
            this.BeginInit();
            if (frame.FrameIndex < 0 || frame.FrameIndex >= sources.Count) return;
            BitmapSource source = sources[frame.FrameIndex];

            if (image.ImageSource != source)
            {
                image.ImageSource = source;
                this.Width = source.PixelWidth;
                this.Height = source.PixelHeight;
            }

            Canvas.SetLeft(this, frame.PosX - source.PixelWidth / 2);
            Canvas.SetTop(this, frame.PoxY - source.PixelHeight);
            Canvas.SetZIndex(this, frame.ZIndex);
            this.EndInit();
        }
    }
}
